package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityShareMagessLandBinding;
import com.sgb.knowledgeshare.entity.ReceiveLaudEntity;
import com.sgb.knowledgeshare.entity.VideoReceiveListEntity;
import com.sgb.knowledgeshare.view.adapter.MagessLandAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareMagessLandModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;
import java.util.List;


/**
 * Created by fq on 2020/9/24 0024
 * Description： 分享-消息-赞
 */
public class ShareMagessLandActivity  extends AppActivity {

    ShareMagessLandModel viewModel;
    private ActivityShareMagessLandBinding binding;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private MagessLandAdapter magesslandadapter;
    private int page;
    private boolean isLoadMore;
    private List<ReceiveLaudEntity.RecordsBean> ReceivelLaudlist;
    private VideoReceiveListEntity.RecordsBean bean;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ShareMagessLandActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share_magess_land);
        viewModel = new ViewModelProvider(this).get(ShareMagessLandModel.class);
        viewModel.setActivity(ShareMagessLandActivity.this);
        bean = new VideoReceiveListEntity.RecordsBean();
        View decor = getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.include.titleView.setText("赞");
        binding.include.ivBack.setOnClickListener(this);
        magesslandadapter = new MagessLandAdapter(this,null);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(magesslandadapter);
        DividerDecoration divider = new DividerDecoration.Builder(getApplicationContext())
                .setHeight(R.dimen.dp5)
                .setPadding(R.dimen.dp0)
                .setColorResource(R.color.white)
                .build();
        binding.lrecyclerview.addItemDecoration(divider);
        binding.lrecyclerview.setLayoutManager(new LinearLayoutManager(this));
        binding.lrecyclerview.setAdapter(mLRecyclerViewAdapter);
        binding.lrecyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
        binding.lrecyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page++;
                viewModel.getReceivelLaud(page);
            }
        });
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                String videoIds =  magesslandadapter.getDatas().get(position).getVideoId();
                ReceiveLaudEntity.RecordsBean recordsBean = magesslandadapter.getDatas().get(position);
                if (recordsBean.getType()==2){
                    bean.setCommentId(recordsBean.getCommentId());
                    bean.setVideoId(videoIds);
                }else {
                    bean.setCommentId("");
                }
                ShareLandActivity.start(getApplicationContext(),videoIds,"video",bean);
            }
        });
    }

    @Override
    protected void init() {
        super.init();
       initData();
    }

    private void initData() {
        isLoadMore = false;
        page = 1;
        viewModel.getReceivelLaud(page);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }

    public void setReceivelLaud(ReceiveLaudEntity data) {
        if (data ==null){
            return;
        }

        binding.lrecyclerview.refreshComplete(page);
        binding.lrecyclerview.setPullRefreshEnabled(true);
        if (isLoadMore) {
            if (data.getRecords().size() > 0) {
                magesslandadapter.addDatas(data.getRecords());
            } else {
                MToast.showToast(getApplicationContext(), "没有数据啦");
            }
        } else {
            if (data.getRecords().size() > 0) {
                binding.lrecyclerview.setVisibility(View.VISIBLE);
                binding.empty.setVisibility(View.GONE);
                magesslandadapter.setDatas(data.getRecords());
            } else {
                binding.lrecyclerview.setVisibility(View.GONE);
                binding.empty.setVisibility(View.VISIBLE);
                magesslandadapter.setDatas(null);
            }
        }
        this.ReceivelLaudlist = magesslandadapter.getDatas();
    }
}
